#include "../include/mytools.h"

int judgefile(char * filepath)
{
	struct stat s;
	if (stat(filepath, &s) == 0)
    {
        if (s.st_mode & __S_IFDIR)
        {
            return 2;
        }
        else if (s.st_mode & __S_IFREG)
        {
            return 1;
        }
        else
            return 0;
    }
    return -1;
}

long get_file_size(char *file_name)
{
	assert(file_name != NULL);
	if(access(file_name, F_OK))
	{
		printf("file: %s can not open!\n", file_name);
		return false;
	}

	FILE *fp = fopen(file_name, "rb");
	if(NULL == fp )
	{
		printf("file: %s can not open!\n", file_name);
		return false;
	}

	long file_size = 0;
	fseek(fp, 0, SEEK_END); //把文件内部指针移动到文件尾部
	file_size = ftell(fp); //返回指针偏离文件头的位置(即文件中字符个数)
	fseek(fp, 0, SEEK_SET); //把文件内部指针移回到文件头部

	fclose(fp);

	return file_size;
}

int is_exist_file(char * filepath, char * filename)
{
    DIR *p_dir = NULL;
 
    //定义一个目录结构体指针
    struct dirent *p_dirent = NULL;
 
    //打开目录，返回一个目录流指针指向第一个目录项
    p_dir = opendir(filepath);
    if(p_dir == NULL)
    {
        printf("opendir error\n");
    }
 
    //循环读取每个目录?, 当返回NULL时读取完?
    while((p_dirent = readdir(p_dir)) != NULL)
    {
        if(!strcmp(p_dirent->d_name, ".") || !strcmp(p_dirent->d_name, ".."))
            continue;
        if(!strcmp(filename, p_dirent->d_name))
        {
            char filepath_name[MAX_SIZE_OF_FILE_NAME]={0};
            strcpy(filepath_name, filepath);
            strcat(filepath_name, "/");
            strcat(filepath_name, filename);
            return judgefile(filepath_name);
        }
    }
    return -1;

}

int TraverseDir(char *direntName, int level, bool flag)
{
    //定义一个目录流指针
    DIR *p_dir = NULL;
 
    //定义一个目录结构体指针
    struct dirent *p_dirent = NULL;
    
    int totolsize = 0;
    //打开目录，返回一个目录流指针指向第一个目录项
    p_dir = opendir(direntName);
    if(p_dir == NULL)
    {
        return printf("opendir error\n");
    }
 
    //循环读取每个目录?, 当返回NULL时读取完?
    while((p_dirent = readdir(p_dir)) != NULL)
    {
        //备份之前的目录名
        char *backupDirName = NULL;
		
        if(!strcmp(p_dirent->d_name, ".") || !strcmp(p_dirent->d_name, ".."))
            continue;
        if(!flag)
        {
            if(p_dirent->d_name[0] == '.')
                continue;
        }
        int i;
        for(i = 0; i < level; i++)
        {
            totolsize += printf("    |");
        }
        totolsize += printf("    %s\n", p_dirent->d_name);
 
        //如果目录项仍是一个目录的话，进入目录
        if(p_dirent->d_type == DT_DIR)
        {
            //当前目录长度
            int curDirentNameLen = strlen(direntName)+strlen(p_dirent->d_name) + 2;
 
            //准备下一个?
            backupDirName = (char *)malloc(curDirentNameLen);
            memset(backupDirName, 0, curDirentNameLen);
            memcpy(backupDirName, direntName, curDirentNameLen);
 
            strcat(backupDirName, "/");
            strcat(backupDirName, p_dirent->d_name);
            totolsize += TraverseDir(backupDirName, level + 1, flag);//递归调用
 
            free(backupDirName);
            backupDirName = NULL;
        }
    }
    
    closedir(p_dir);
    return totolsize;
}

bool is_ipv4_addr(char *ip)
{
    if(!strcmp(ip, "localhost") || !strcmp(ip, "0.0.0.0"))return true;
	if (ip == NULL || ip[0] == '0' || ip[0] == '\0') {
		return false;
	}
	for (int i = 0, count = 0; i < strlen(ip); i++) {
		if ((ip[i] != '.') && (ip[i] < '0' || ip[i] > '9')) {
			return false;
		}
		if (ip[i] == '.') {
			count++;
			if (count > 3) {
				return false;
			}
		}
	}

	int ip_num[4] = {-1, -1, -1, -1};
	char ip_s[4][4];
	memset(ip_s, 0, sizeof(char[4]) * 4);

	sscanf(ip, "%[^.].%[^.].%[^.].%[^ ]", ip_s[0], ip_s[1], ip_s[2], ip_s[3]);
	sscanf(ip_s[0], "%d", &ip_num[0]);
	sscanf(ip_s[1], "%d", &ip_num[1]);
	sscanf(ip_s[2], "%d", &ip_num[2]);
	sscanf(ip_s[3], "%d", &ip_num[3]);

	for (int i = 0; i < 4; i++) {
		if (strlen(ip_s[i]) == 0 || (ip_s[i][0] == '0' && ip_s[i][1] != '\0') || ip_num[i] < 0 || ip_num[i] > 255) {
			return false;
		}
	}

	return true;
}

bool judgepath(char *path)
{
    // 遍历路径判断是否超出范围，即..是否过多，如果超出范围则返回false
    if(path == NULL || path[0] == '\0')
        return false;
    char *p = path;
    int count = 0;
    int start = 0;
    int end = 0;
    while(*p != '\0')
    {
        if(*p == '/')
        {
            if(start == end) ;
            else if(start == end - 1 && path[start] == '.') ;
            else if(start == end - 2 && path[start] == '.' && path[start + 1] == '.') count++;
            else count--;
            if(count > 0) return false;
            start = end + 1;
            end = start;
        }
        else end++;
        p++;
    }
    if(count > 0)
        return false;
    return true;
}
